<!DOCTYPE html>

<html>
	<head>
		<title>Line</title>
		<script type="text/javascript" src="../libs/three.js"></script>
		<script type="text/javascript" src="../libs/jquery-1.9.0.js"></script>
		<script type="text/javascript" src="../libs/stats.js"></script>
		<script type="text/javascript" src="../libs/dat.gui.js"></script>
		<script type="text/javascript" src="../libs/OrbitControls.js"></script>
		<style >
			body{
				/* set margin to 0 and overflow to hidden, to use the complete page */

				margin: 0;
				overflow: hidden;
			}
		</style>
	</head>
	<body>

		<!-- Div which will hold the Output -->
		<div id="WebGL-output">
		</div>

		<!-- Div which will show the stats -->
		<div id="Stats-output">
		</div>

		<!-- Javascript code that runs our Three.js examples -->
		<script type="text/javascript">

			var orbit;
			
			var mouse = new THREE.Vector2();
			var mousex, mousey;

			//once everything is loaded, we run our Three.js stuff.
			$(function () { 
 				var stats = initStats();
 				
 				//here we'll put the Three.js stuff
 				var scene = new THREE.Scene();
 				
 				//add camera and renderer
 				var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
 				
 				var renderer = new THREE.WebGLRenderer();
 				renderer.setClearColor(0xEEEEEE, 1.0);
 				renderer.setSize(window.innerWidth, window.innerHeight);
 				renderer.shadowMapEnabled = true;

 				//add Light
 				var ambientLight = new THREE.AmbientLight(0x0c0c0c);
 				scene.add(ambientLight);

 				var spotLight = new THREE.SpotLight(0xffffff);
 				spotLight.castShadow = true;
 				spotLight.position.set(-40, 60, -10);
 				scene.add(spotLight);

 				//add plane
 				var planeGeo = new THREE.PlaneGeometry(60,40,1,1);
 				var planeMtr = new THREE.MeshLambertMaterial({
 					color: 0xffffff,
 					side: THREE.DoubleSide
 				});
 				var plane = new THREE.Mesh(planeGeo, planeMtr);
 				plane.rotation.x = -0.5 * Math.PI;
		        plane.position.x = 0;
		        plane.position.y = 0;
		        plane.position.z = 0;
		        plane.receiveShadow = true;
		        // scene.add(plane);


		        camera.position.x = -30;
		        camera.position.y = 40;
		        camera.position.z = 30;
		        camera.lookAt(scene.position);
		        document.getElementById("WebGL-output").appendChild(renderer.domElement);

		        orbit = new THREE.OrbitControls(camera, renderer.domElement);

		        document.addEventListener( 'mousemove', onDocumentMouseMove, false );
		        window.addEventListener( 'resize', onWindowResize, false );

		        var lines = new THREE.Geometry();

		        lines.vertices.push(
					new THREE.Vector3( -10, 0, 0 ),
					new THREE.Vector3( 0, 10, 0 ),
					new THREE.Vector3( 10, 0, 0 )
				);
				lines.vertices.push(new THREE.Vector3( 0, 0 ,10));

		        var material = new THREE.LineBasicMaterial({
		            opacity: 1.0,
		            linewidth: 1,
		            vertexColors: THREE.VertexColors
		        });

		        // var line = new THREE.Line(lines, material);
		        // scene.add(line);

 				//add animation
 				render();
 				function render(){
 					stats.update();
 					orbit.update();

 					requestAnimationFrame(render);
 					renderer.render(scene, camera);
 				};

 				function onWindowResize() {

					camera.aspect = window.innerWidth / window.innerHeight;
					camera.updateProjectionMatrix();

					renderer.setSize( window.innerWidth, window.innerHeight );

				}

				function onDocumentMouseMove( event ) {

					event.preventDefault();

					mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
					mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

				}

				window.onclick = function ( event){
					scene.remove(line);
					mousex = mouse.x;
					mousey = mouse.y;
					lines.vertices.push(new THREE.Vector3(mousex * 10, mousey * 10, 0));

					var line = new THREE.Line(lines, material);
					console.log(line);
					scene.add(line);

				}

			});

			function initStats(){
				var stats = new Stats();
				stats.setMode(0);
				stats.domElement.style.position = 'absolute';
				stats.domElement.style.lefe = '0px';
				stats.domElement.style.top = '0px';
				$("#Stats-output").append(stats.domElement);
				return stats;
			}



		</script>

	</body>
</html>